package com.tyut.homework.service.impl;

import com.tyut.homework.pojo.Registration;
import com.tyut.homework.repository.RegisRepository;
import com.tyut.homework.service.RegistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class RegisServiceImpl implements RegistService {

    @Autowired
    private RegisRepository regisRepository;
    @Override
    public Registration save(Registration registration) {
        return regisRepository.save(registration);
    }

    @Override
    public int delete(Integer id) {
        Registration registration = new Registration();
        registration.setId(id);
        regisRepository.delete(registration);
        return 1;
    }

    @Override
    public int update(Registration registration) {
        regisRepository.saveAndFlush(registration);
        return 1;
    }

    @Override
    public Registration find(Integer id) {
        Optional<Registration> byId = regisRepository.findById(id);
        return byId.get();
    }

    @Override
    public List<Registration> findAll() {
        return null;
    }

    @Override
    public Page<Registration> getPage(Integer pageNum, Integer pageLimit) {
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(pageNum, pageLimit, sort);
        return regisRepository.findAll(pageable);
    }
}
